package com.mavenq.fly.business.controller;

import com.mavenq.fly.annotation.Log;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Log(title = "用户登录", operation = "login")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        if ("admin".equals(loginDTO.getUsername())) {
            return Result.success("登录成功");
        }
        return Result.fail("用户名或密码错误");
    }

    @Log(title = "获取用户信息", operation = "getUserInfo")
    @GetMapping("/{id}")
    public Result getUserInfo(@PathVariable Long id) {
        UserVO user = new UserVO();
        user.setId(id);
        user.setUsername("admin");
        user.setNickname("管理员");
        return Result.success(user);
    }

    @Log(title = "导出用户数据", operation = "exportUsers",
         maxResultLength = 500)
    @GetMapping("/export")
    public Result exportUsers() {
        return Result.success(new UserVO[]{new UserVO(1L, "admin"), new UserVO(2L, "user1")});
    }

    @Log(title = "删除用户", operation = "deleteUser")
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Long id) {
        if (id > 100) {
            throw new RuntimeException("用户不存在");
        }
        return Result.success("删除成功");
    }
}

@Data
class LoginDTO {
    private String username;
    private String password;
}

@Data
class UserVO {
    private Long id;
    private String username;
    private String nickname;

    public UserVO() {}

    public UserVO(Long id, String username) {
        this.id = id;
        this.username = username;
    }
}

@Data
class Result {
    private int code;
    private String msg;
    private Object data;

    public static Result success(Object data) {
        Result result = new Result();
        result.code = 200;
        result.msg = "success";
        result.data = data;
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result();
        result.code = 500;
        result.msg = msg;
        return result;
    }
}